﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;
using System.Web;
using System.IO;

namespace MTA.TripPlanner
{
    public class FeedUrlBuilder : NameValueCollection
    {
        public String PageName { get; set; }
        public FeedUrlBuilder(String pageName)
        {
            this.PageName = pageName;
        }

        public string GetUrl()
        {
            String _url = String.Format("{0}{1}?apikey={2}&format=json", ConfigurationManager.AppSettings["MTAFeedBase"],
                PageName,
                ConfigurationManager.AppSettings["ApiKey"]);

            foreach (String key in Keys)
            {
                _url += "&";
                _url += String.Format("{0}={1}", key, HttpUtility.UrlEncode(this[key]));
            }
            return _url;
        }

        public String GetCachePage()
        {
            String cacheDir = ConfigurationManager.AppSettings["CacheDir"];
            String dir = (HttpContext.Current == null) ?  cacheDir : HttpContext.Current.Server.MapPath(cacheDir);
            String path = Path.Combine(dir, PageName.Replace(".php", String.Empty));
            
            foreach (String key in Keys)
            {
                path += String.Format("{0}{1}", key, this[key].ToString());
            }
            return path + ".txt";
        }
    }
}
